{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Coin Flip: A Probabilistic Bit</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1: Simulating FairCoin in Python</h3>\n",
    "\n",
    "Flip a fair coin 100 times. Calcuate the total number of heads and tails, and then compare them.\n",
    "\n",
    "Do the same experiment 1000 times.\n",
    "\n",
    "Do the same experiment 10,000 times.\n",
    "\n",
    "Do the same experiment 100,000 times.\n",
    "\n",
    "Do your results get close to the ideal case (the numbers of heads and tails are the same)?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "for experiment in [100,1000,10000,100000]:\n",
    "    heads = tails = 0\n",
    "    for i in range(experiment):\n",
    "        if randrange(2) == 0: heads = heads + 1\n",
    "        else: tails = tails + 1\n",
    "    print(\"experiment:\",experiment)\n",
    "    print(\"the ratio of #heads/#tails is\",(heads/tails),\"heads =\",heads,\"tails = \",tails)\n",
    "    print() # empty line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2: Simulating BiasedCoin in Python</h3>\n",
    "\n",
    "Flip the following biased coin 100 times. Calcuate the total number of heads and tails, and then compare them.\n",
    "\n",
    "$\n",
    "BiasedCoin = \\begin{array}{c|cc} & \\mathbf{Head} & \\mathbf{Tail} \\\\ \\hline \\mathbf{Head} & 0.6 & 0.6 \\\\  \\mathbf{Tail} & 0.4 & 0.4  \\end{array}\n",
    "$\n",
    "\n",
    "\n",
    "Do the same experiment 1000 times.\n",
    "\n",
    "Do the same experiment 10,000 times.\n",
    "\n",
    "Do the same experiment 100,000 times.\n",
    "\n",
    "Do your results get close to the ideal case $ \\mypar{ \\dfrac{ \\mbox{# of heads} }{ \\mbox{# of tails} } = \\dfrac{0.6}{0.4} = 1.50000000 } $?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3>Solution</h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "\n",
    "# let's pick a random number between {0,1,...,99}\n",
    "# it is expected to be less than 60 with probability 0.6\n",
    "#     and greater than or equal to 60 with probability 0.4\n",
    "\n",
    "for experiment in [100,1000,10000,100000]:\n",
    "    heads = tails = 0\n",
    "    for i in range(experiment):\n",
    "        if randrange(100) <60: heads = heads + 1 # probability with 0.6\n",
    "        else: tails = tails + 1 # probability with 0.4\n",
    "    print(\"experiment:\",experiment)        \n",
    "    print(\"the ratio of #heads/#tails is\",(heads/tails),\"heads =\",heads,\"tails = \",tails)\n",
    "    print() # empty line"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
